package com.topscomm.mis.cbb.pm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmDeliverableEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmRiskEntity;
import com.topscomm.mis.cbb.pm.service.abs.AbstractRiskService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
public class RiskService extends AbstractRiskService {

    /**
     * description 新增前处理，自动生成单号
     *
     * @param paramMap
     * @return void
     * @author wxm
     * @date 2023-06-26 10:42:43
     * @modify
     */
    @Override
    protected void beforeInsert(Map<String, Object> paramMap) throws ServiceException {

        // 获取id并赋值
        long[] arrayId = PrimaryKeyUtil.GeneIDsFromCache(CbbPmRiskEntity.tableName, 1);

        // 赋值单号
        if (ConvertUtil.convertToLong(paramMap.get(CbbPmRiskEntity.FieldId)) <= 0) {
            paramMap.put(CbbPmDeliverableEntity.FieldId, arrayId[0]);
            String code = String.valueOf(arrayId[0]);
            code = "FX" + code.substring(0, 6) + code.substring(9);
            paramMap.put(CbbPmDeliverableEntity.FieldCode, code);
        }
    }

    /**
     * description 展示提交人
     *
     * @param mapRowList
     * @return void
     * @author wxm
     * @date 2023-06-25 18:39:38
     * @modify
     */
    @Override
    protected void setDispFields(List<Map<String, Object>> mapRowList) {

        if (CollectionUtils.isEmpty(mapRowList)) {
            return;
        }
        // 责任人
        for (Map<String, Object> map : mapRowList) {
            map.put("riskowner",
                ConvertUtil.convertToString(map.get(CbbPmRiskEntity.FieldRiskownername)) + "["
                    + ConvertUtil.convertToString(map.get(CbbPmRiskEntity.FieldRiskownercode))
                    + "]");
        }
    }

    /**
     * description 生成单号
     *
     * @param basicEntityList
     * @return void
     * @author wxm
     * @date 2023-06-27 22:05:39
     * @modify
     */
    @Override
    protected void beforeInsertBatch(List<BasicEntity> basicEntityList) {

        long[] arrayId = PrimaryKeyUtil
            .GeneIDsFromCache(CbbPmRiskEntity.tableName, basicEntityList.size());
        Date currentDate = new Date();
        for (int i = 0; i < basicEntityList.size(); i++) {
            CbbPmRiskEntity riskEntity = (CbbPmRiskEntity) basicEntityList.get(i);
            riskEntity.setId(arrayId[i]);
            String code = String.valueOf(arrayId[i]);
            code = "FX" + code.substring(0, 6) + code.substring(9);
            riskEntity.setCode(code);
            riskEntity.setCreateon(currentDate);
            riskEntity.setModifiedon(currentDate);
        }
    }
}